﻿using GuoZhiQi.MiddleCache.Object.Atomic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GuoZhiQi.MiddleCache.Parameter
{
    public abstract class CouchBaseParameter<T> : CouchBaseAtomic<T>, ICouchBaseParameter<T>
    {
        #region Parent property.

        private string _key;

        /// <summary>
        /// Key of this parameter. Includes parent key.
        /// </summary>
        public override string Key
        {
            get
            {
                if (this.Parent == null)
                    return null;

                return this._key;
            }
            set
            {
                if (this.Parent == null)
                    return;

                if (!value.StartsWith(this.Parent.Key + Global.ParentChildDelimiter))
                    this._key = this.Parent.Key + Global.ParentChildDelimiter + value;
                else
                    this._key = value;
            }
        }

        /// <summary>
        /// Parent of this parameter
        /// </summary>
        public virtual ICouchBaseObject Parent
        {
            get;
            private set;
        }

        #endregion Parent property.

        #region Main value.

        /// <summary>
        /// Value of this parameter.
        /// </summary>
        public override abstract T Value
        {
            get;
            set;
        }

        #endregion Main value.

        #region Constructor

        /// <summary>
        /// Constructor of this parameter with object owner.
        /// </summary>
        /// <param name="Owner">Owner of this parameter</param>
        /// <param name="paramKey">Part of the key to uniquely identify child. Should not be reused as full key. Insted call <see cref="MembaseParameter<T>.Key"/>.</param>
        public CouchBaseParameter(CouchBaseObject owner, string paramKey)
            : base(owner.Client)
        {
            if (owner == null)
                throw new ArgumentNullException("owner");

            this.Parent = owner;
            this.Key = paramKey;
        }

        #endregion Constructor
    }
}
